package com.vhuomai.wechat.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.system.constant.UserRole;
import com.vhuomai.wechat.entity.BaseWxMenu;
import com.vhuomai.wechat.service.WxMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "微信公众号菜单")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class WxMenuController {

    @Autowired
    private WxMenuService menuService;

    @ApiOperation(value = "获取菜单列表")
    @GetMapping(value = "/wechat/menus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseWxMenu>> menus() {
        return ServiceResult.success(menuService.findAll());
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping(value = "/wechat/menu/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseWxMenu> addMenu(@RequestBody BaseWxMenu menu) {
        return ServiceResult.success(menuService.add(menu));
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping(value = "/wechat/menu/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateMenu(@RequestBody BaseWxMenu menu) {
        menuService.update(menu);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping(value = "/wechat/menu/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> removeMenu(@RequestParam("id") Integer menuId) {
        menuService.remove(menuId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "同步菜单到微信")
    @PutMapping(value = "/wechat/menu/sync", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> menuSyncToWx() {
        menuService.syncToWx();
        return ServiceResult.SUCCESS;
    }
}
